package com.woniu.liuzhougov.authorityControl.controller;

import com.woniu.liuzhougov.authorityControl.pojo.Rights;
import com.woniu.liuzhougov.authorityControl.service.RightsService;
import com.woniu.liuzhougov.authorityControl.service.RoleRightsService;
import com.woniu.liuzhougov.authorityControl.utils.TableResult;
import com.woniu.liuzhougov.authorityControl.utils.TreeResult;
import com.woniu.liuzhougov.authorityControl.verification.AddRights;
import com.woniu.liuzhougov.authorityControl.verification.EditRights;
import com.woniu.liuzhougov.common.utils.Result;
import com.woniu.liuzhougov.common.utils.ValidatedResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhuxinpan
 * @version 1.0
 * @className RightsController
 * @description 处理和权限相关请求
 * @date 2019/4/29 11:45
 */
@Controller
@RequestMapping("/rights")
public class RightsController {
    @Resource
    private RightsService rightsService;

    @Resource
    private RoleRightsService roleRightsService;

    /**
     * @param
     * @return java.lang.String
     * @author zhuxinpan
     * @date 2019/4/29 12:12
     * @description 跳转到权限管理页面
     */
    @RequiresPermissions("rights_manager")
    @GetMapping("/manager")
    public String manager() {
        return "rights_manager";
    }

    /**
     * @param
     * @return com.woniu.liuzhougov.authorityControl.utils.TableResult<com.woniu.liuzhougov.authorityControl.pojo.Rights>
     * @author zhuxinpan
     * @date 2019/4/29 15:58
     * @description 列出所有权限
     */
    @GetMapping("/listRights")
    @ResponseBody
    public TableResult listRights() {
        List<Rights> rightsList = rightsService.listRights();
        TableResult tableResult = TableResult.getTableResult(rightsList, (long) rightsList.size());
        return tableResult;
    }

    /**
     * @param parentId
     * @return java.lang.String
     * @author zhuxinpan
     * @date 2019/4/29 19:07
     * @description 跳转到添加资源界面
     */
    @RequiresPermissions("rights_addRights")
    @GetMapping("/addRights/{parentId}")
    public String addRights(@PathVariable(name = "parentId") Integer parentId, Model model) {
        model.addAttribute(parentId);
        return "rights_manager_addRights";
    }

    /**
     * @param rights
     * @param bindingResult
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/29 19:38
     * @description 添加资源
     */
    @RequiresPermissions("rights_addRights")
    @PostMapping("/addRights")
    @ResponseBody
    public Result addRights(@Validated(value = {AddRights.class}) Rights rights, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(ValidatedResult.getValidateMessage("添加资源失败!", bindingResult));
        }
        if ("".equals(rights.getRightsName().trim())) {
            return Result.error("资源名不能为空!");
        }
        rights.setRightsId(null);
        if (rightsService.countByRightsName(rights) > 0) {
            return Result.error("该资源名称已存在!");
        }
        if (rightsService.countNumberByPerms(rights) > 0) {
            return Result.error("该权限标识已经存在");
        }
        if (rightsService.insertRights(rights) == 0) {
            return Result.error("添加资源失败!");
        }
        return Result.success("添加资源成功!");
    }

    /**
     * @param rightsId
     * @param model
     * @return java.lang.String
     * @author zhuxinpan
     * @date 2019/4/29 19:49
     * @description 跳转到编辑页面
     */
    @RequiresPermissions("rights_editRights")
    @GetMapping("/editRights/{rightsId}")
    public String editRights(@PathVariable(name = "rightsId") Integer rightsId, Model model) {
        Rights rights = rightsService.getRightsByRightsId(rightsId);
        model.addAttribute("rights", rights);
        return "rights_manager_editRights";
    }

    /**
     * @param rights
     * @param bindingResult
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/29 19:50
     * @description 修改资源
     */
    @RequiresPermissions("rights_editRights")
    @PostMapping("/editRights")
    @ResponseBody
    public Result editRights(@Validated(value = {EditRights.class}) Rights rights, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(ValidatedResult.getValidateMessage("修改资源失败!", bindingResult));
        }
        if ("".equals(rights.getRightsName().trim())) {
            return Result.error("资源名不能为空!");
        }
        if (rightsService.countByRightsName(rights) > 0) {
            return Result.error("该资源名称已存在!");
        }
        if (rightsService.countNumberByPerms(rights) > 0) {
            return Result.error("该权限标识已经存在");
        }
        if (rightsService.updateRightsByRightsId(rights) == 0) {
            return Result.error("修改资源失败!");
        }
        return Result.success("修改资源成功!");
    }

    /**
     * @param rightsId
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/29 20:16
     * @description 删除权限
     */
    @RequiresPermissions("rights_deleteRights")
    @DeleteMapping("/deleteRights/{rightsId}")
    @ResponseBody
    public Result deleteRights(@PathVariable(name = "rightsId") Integer rightsId) {
        if (roleRightsService.countNumberByRightsId(rightsId) > 0 || rightsService.countNumberByParentId(rightsId) > 0) {
            return Result.error("该资源再被使用，不能被删除!");
        }
        if (rightsService.deleteRightsByRightsId(rightsId) > 0) {
            return Result.success("删除资源成功!");
        }
        return Result.error("删除资源失败!");
    }

    /**
     * @param roleId
     * @param model
     * @return java.lang.String
     * @author zhuxinpan
     * @date 2019/4/29 20:47
     * @description 跳转到权限设置界面
     */
    @RequiresPermissions("rights_setAuthorization")
    @GetMapping("/setAuthorization/{roleId}")
    public String setAuthorization(@PathVariable(name = "roleId") Integer roleId, Model model) {
        List<Rights> rightsList = rightsService.listRights();
        List<Integer> rightsCheckedList = roleRightsService.listRightsIdByRoleId(roleId);
        Integer[] checkedList = new Integer[rightsCheckedList.size()];
        rightsCheckedList.toArray(checkedList);
        Map<String, Object> data = new HashMap<>();
        data.put("list", rightsList);
        data.put("checkedId", checkedList);
        model.addAttribute("treeResult", TreeResult.getTreeResult(data));
        model.addAttribute(roleId);
        return "role_manager_setAuthorization";
    }

    /**
     * @param roleId
     * @param rightsIds
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/29 21:03
     * @description 设置角色权限
     */
    @RequiresPermissions("rights_setAuthorization")
    @PostMapping("/setAuthorization")
    @ResponseBody
    public Result setAuthorization(Integer roleId,Integer[] rightsIds) {
        if (rightsIds == null) {
            rightsIds = new Integer[0];
        }
        if (roleRightsService.setAuthorization(roleId, rightsIds) == 0) {
            return Result.error("设置角色失败");
        }
        return Result.success("设置角色成功");
    }


}
